-- BiggerContainers
-- by eLe
-- Upgraded by WrathOf to not use overrides
--
--     Note: this should not have any incompatibilites unless another mod
--           attempts to do the same change.
--     Note: If this mod is not loaded when a saved game that used it is loaded,
--           all items in the expanded slots *should* get dropped on the ground.
--           Needs to be verified though.
-------------------------------------------------------------------------------
-- Description:
--
-- Increases container size of chester, chest, icebox, backpack, krampus sack.
--
-------------------------------------------------------------------------------
-- Support thread here:
--
-- http://forums.kleientertainment.com/showthread.php?12699
--
-------------------------------------------------------------------------------
--
-- To install this mod, put this file in a new folder under
-- "...\Steam\steamapps\common\dont_starve\mods" called "BiggerContainers",
-- then open the "modsettings.lua" file in the mods folder and add the
-- following but without the beginning "--":
--
-- ModManager:AddMod("BiggerContainers")
--
-- When you run the game you should see a screen pop up that lists this mod as
-- being loaded.
--
-------------------------------------------------------------------------------
--
-- Changelog:
--
-- v0.9 End Update.
--		Upped Backpack to 3x4 (12 slots), Pigpack to 3x5 (15 slots), and Krampus Bag to 3x6 (18 slots).
--		Code adjustment to make it easier to move the ui location.
--		Adjusted ui images.
--
-- v0.8 Changed backpack sizes: backpack is 3x3 (9 slots), pigback is 3x4 (12 slots) and krampus sack is 3x5 (12 slots)
--		Removed whitespace from the 3x4 chest ui. It is now 308x416.
--
-- v0.7 Fixed background image so they do not overlap when multiple containers are open at once.
--
-- v0.6 "Fixed" bug where you can't access the inventory slots under the chester ui by moving the ui to the left of the chest ui.
--		Added \scripts\widgets\containerwidget.lua from modKittiko Bag as per WrathOf.
--		Added larger 12 slot icebox.
--		Moved chect ui up by one row.
--		Moved icebox ui to right of the chest ui.
--
-- v0.5 Fixed prefab.xml code conflict.
--
-- v0.4 Thanks to WrathOf (again :D) I was able to make a ui background for all of the vanilla containers.
--
-- v0.3 Embiggened Krampus Sack to 3x5 (15 slots), all other containers are 3x4 (12 slots).
--		 Removed background.
--
-- v0.2 WrathOf could not help himself and changed it to not require overrides :P
--
-- v0.1 Increased container size of chester, chest, backpack, krampus sack.
-------------------------------------------------------------------------------

print("Loading Bigger Container Mod....")

GLOBAL.TheSim:LoadPrefabDefs( MODROOT.."prefabs.xml" )

--Add local version of global for convenience
Vector3 = GLOBAL.Vector3


--
-- Future: Make general function to handle calculation of vectors so code is more readable
--         and easier to adjust container screen positions, etc.
--

--bags
local function backpackPrefabPostInit(inst)

	--Always make sure we have a component before attempting to use it.
	--Never assume anything now that modding is supported.
	--Makes for happy players.
	if inst.components.container then

		local slotpos = {}

		--[[Adds 2 rows, 1 above and one below center line
		for y = -1, 4 do --changed from y = 0, 3
			table.insert(slotpos, Vector3(-162, -y*75 + 114 ,0))
			table.insert(slotpos, Vector3(-162 +75, -y*75 + 114 ,0))
		end
		--]]

		--Alternative would be to add another column, adjusted x so new column toward center of screen
		for y = 0, 3 do		--4 rows, 3 cols
			--						([hor] [loc], [hor width between rows] + [ver] ,0))					
			table.insert(slotpos, Vector3(-96 -150, -y*75 + 56 ,0)) --New column
			table.insert(slotpos, Vector3(-96  -75, -y*75 + 56 ,0)) --original column
			table.insert(slotpos, Vector3(-96     , -y*75 + 56 ,0)) --col at screen edge
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_backpack_2x4"
		--inst.components.container.widgetpos = Vector3(645,-85,0)
		--give non-animated background
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_bag_3x4.tex"
		inst.components.container.widgetbgimagepos = Vector3(-85, -109, 0)
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end

local function pbackPrefabPostInit(inst)

	if inst.components.container then

		local slotpos = {}

		--[[Adds 2 rows, 1 above and one below center line
		for y = -1, 5 do --changed from y = 0, 4
			table.insert(slotpos, Vector3(-162,     -y*75 + 114 ,0))
			table.insert(slotpos, Vector3(-162 +75, -y*75 + 114 ,0))
		end
		--]]

		--Alternative would be to add another column
		for y = 0, 4 do		--5 rows, 3 cols
			table.insert(slotpos, Vector3(-96 -150, -y*75 + 56 ,0)) --New column
			table.insert(slotpos, Vector3(-96  -75, -y*75 + 56 ,0)) --original column
			table.insert(slotpos, Vector3(-96     , -y*75 + 56 ,0)) --col at screen edge
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_krampusbag_2x5"
		--inst.components.container.widgetpos = Vector3(645,-85,0)
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_bag_3x5.tex"
		inst.components.container.widgetbgimagepos = Vector3(-85, -129, 0)
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end

local function ksackPrefabPostInit(inst)

	if inst.components.container then

		local slotpos = {}

		--[[Adds 2 rows, 1 above and one below center line
		for y = -1, 5 do --changed from y = 0, 4
			table.insert(slotpos, Vector3(-162,     -y*75 + 114 ,0))
			table.insert(slotpos, Vector3(-162 +75, -y*75 + 114 ,0))
		end
		--]]

		--Alternative would be to add another column
		for y = 0, 5 do		--5 rows, 3 cols
			table.insert(slotpos, Vector3(-96 -150, -y*75 + 98 ,0)) --New column
			table.insert(slotpos, Vector3(-96  -75, -y*75 + 98 ,0)) --original column
			table.insert(slotpos, Vector3(-96     , -y*75 + 98 ,0)) --col at screen edge
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_krampusbag_2x5"
		--inst.components.container.widgetpos = Vector3(645,-85,0)
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_bag_3x6.tex"
		inst.components.container.widgetbgimagepos = Vector3(-85, -88, 0)
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end
--/bags

--chests
local function chesterPrefabPostInit(inst)

	if inst.components.container then

		local slotpos = {}
        for y = 3, 0, -1 do --changed from y = 2, 0, -1; IMPORTANT: DO NO CHANGE LAST NUMBER WILL CAUSE CRASH
			for x = 0, 2 do			  		 --(hor, vert)
				table.insert(slotpos, Vector3(80*x-80*2-240, 80*y-80*2+713,0))
			end
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_chest_3x3"
		--inst.components.container.widgetpos = Vector3(0,-180,0)
		--give non-animated background
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_chest_3x4.tex"
		inst.components.container.widgetbgimagepos = Vector3(-330,676,0)
		inst.components.container.widgetbgimagetint = {r=.82,g=.73,b=.76,a=1}
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end

local function tchestPrefabPostInit(inst)

	if inst.components.container then

		local slotpos = {}
		for y = 3, 0, -1 do --changed from y = 2, 0, -1; IMPORTANT: DO NO CHANGE LAST NUMBER WILL CAUSE CRASH
			for x = 0, 2 do
				table.insert(slotpos, Vector3(80*x-80*2+80, 80*y-80*2+80,0))
			end
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_chest_3x3"
		--inst.components.container.widgetpos = Vector3(0,200,0)
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_chest_3x4.tex"
		inst.components.container.widgetbgimagepos = Vector3(-10,43,0)
		inst.components.container.widgetbgimagetint = {r=.87,g=.73,b=.66,a=1}
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end

local function iceboxPrefabPostInit(inst)

	if inst.components.container then

		local slotpos = {}
		for y = 3, 0, -1 do --changed from y = 2, 0, -1; IMPORTANT: DO NO CHANGE LAST NUMBER WILL CAUSE CRASH
			for x = 0, 2 do
				table.insert(slotpos, Vector3(80*x-80*2+400, 80*y-80*2+80,0))
			end
		end

		--For future use:
		inst.components.container.widgetanimbank = ""
		--inst.components.container.widgetanimbuild = "ui_chest_3x3"
		--inst.components.container.widgetpos = Vector3(0,200,0)
		inst.components.container.widgetbgimage = "mods/BiggerContainers/images/ui_chest_3x4.tex"
		inst.components.container.widgetbgimagepos = Vector3(310,43,0)
		inst.components.container.widgetbgimagetint = {r=.61,g=.95,b=.99,a=1}
		--Set new container size
		inst.components.container.numslots = #slotpos
		inst.components.container.widgetslotpos = slotpos

	end

end
--/chests

AddPrefabPostInit("backpack", backpackPrefabPostInit)
AddPrefabPostInit("piggyback", pbackPrefabPostInit)
AddPrefabPostInit("krampus_sack", ksackPrefabPostInit)
AddPrefabPostInit("chester", chesterPrefabPostInit)
AddPrefabPostInit("treasurechest", tchestPrefabPostInit)
AddPrefabPostInit("icebox", iceboxPrefabPostInit)